package cn.wolfcode.web.controller.business;

import cn.wolfcode.business.domain.BusAppointment;
import cn.wolfcode.business.service.IBusAppointmentService;
import cn.wolfcode.common.annotation.Log;
import cn.wolfcode.common.core.controller.BaseController;
import cn.wolfcode.common.core.domain.AjaxResult;
import cn.wolfcode.common.core.page.TableDataInfo;
import cn.wolfcode.common.enums.BusinessType;
import cn.wolfcode.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 养修信息预约Controller
 * 
 * @author zhangyf
 * @date 2023-04-11
 */
@RestController
@RequestMapping("/business/appointment")
public class BusAppointmentController extends BaseController
{
    @Autowired
    private IBusAppointmentService busAppointmentService;

    /**
     * 查询养修信息预约列表
     */
    @PreAuthorize("@ss.hasPermi('business:appointment:list')")
    @GetMapping("/list")
    // params[beginTime]: 2023-04-13 00:00:00 ：封装到map里面，key是beginTime，value是2023-04-13 00:00:00（是字符串类型，所以要在BusAppointmentMapper.xml中要把字符串类型转为日期类型）
    // 为什么这里可以接收前端带来的预约时间参数：params[beginTime]和params[endTime]？
    //  因为BusAppointment类实现了BaseEntity类，BaseEntity类中有一个这样的字段 private Map<String, Object> params来封装。
    public TableDataInfo list(BusAppointment busAppointment)
    {
        startPage();
        List<BusAppointment> list = busAppointmentService.selectBusAppointmentList(busAppointment);
        return getDataTable(list);
    }

    /**
     * 导出养修信息预约列表
     */
    @PreAuthorize("@ss.hasPermi('business:appointment:export')")
    @Log(title = "养修信息预约", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusAppointment busAppointment)
    {
        List<BusAppointment> list = busAppointmentService.selectBusAppointmentList(busAppointment);
        ExcelUtil<BusAppointment> util = new ExcelUtil<BusAppointment>(BusAppointment.class);
        util.exportExcel(response, list, "养修信息预约数据");
    }

    /**
     * 获取养修信息预约详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:appointment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(busAppointmentService.selectBusAppointmentById(id));
    }

    /**
     * 新增养修信息预约
     */
    @PreAuthorize("@ss.hasPermi('business:appointment:add')")
    @Log(title = "养修信息预约", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody BusAppointment busAppointment)
    {
        return toAjax(busAppointmentService.insertBusAppointment(busAppointment));
    }

    /**
     * 修改养修信息预约
     */
    @PreAuthorize("@ss.hasPermi('business:appointment:edit')")
    @Log(title = "养修信息预约", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BusAppointment busAppointment)
    {
        return toAjax(busAppointmentService.updateBusAppointment(busAppointment));
    }

    /**
     * 取消养修信息预约
     */
    @PreAuthorize("@ss.hasPermi('business:appointment:cancel')")
    @Log(title = "养修信息预约取消", businessType = BusinessType.UPDATE)
	@PutMapping("/cancel/{id}")
    public AjaxResult cancel(@PathVariable Long id)
    {
        return toAjax(busAppointmentService.cancelBusAppointmentById(id));
    }

    /**
     * 到店养修信息预约
     */
    @PreAuthorize("@ss.hasPermi('business:appointment:arrive')")
    @Log(title = "养修信息预约到店", businessType = BusinessType.UPDATE)
	@PutMapping("/arrive/{id}")
    public AjaxResult arrive(@PathVariable Long id)
    {
        return toAjax(busAppointmentService.arriveBusAppointmentById(id));
    }
}
